﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    LoginBLL studLog;
    LoginProperty studProp = new LoginProperty();
    protected void Page_Load(object sender, EventArgs e)
    {
        studLog = new LoginBLL();
        lblStatus.Text = "";
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        LoginProperty studPropSucc = new LoginProperty();

        bool isFaulty = false;

        string fault = "<a style='color:red;'>";
        lblStatus.Text = fault;
        if (!String.IsNullOrEmpty(txtUserName.Text))
        {
            studProp.Id = txtUserName.Text;
        }
        else
        {
            lblStatus.Text += " Bruger navn mangler! </br>";
            isFaulty = true;
        }

        if (!String.IsNullOrEmpty(txtPasword.Text))
        {
            studProp.Password = txtPasword.Text;
        }
        else
        {
            lblStatus.Text += " Kodeord mangler! </br>";
            isFaulty = true;
        }

        if (!isFaulty)
        {

            studPropSucc = studLog.UserLogin(studProp);
            
            HttpCookie cookie = new HttpCookie("loginCookie");
            cookie.Values.Add("LoginName", studPropSucc.StudentName);
            cookie.Values.Add("LoginMail", studPropSucc.StudentMail);
            cookie.Values.Add("LoginId", studPropSucc.Id);
            cookie.Values.Add("LoginPhone", studPropSucc.StudentPhoneNo);
            cookie.Values.Add("LoginEduc", studPropSucc.SubEducationId.ToString());
            cookie.Values.Add("LoginTerm", studPropSucc.StudTerm);
            cookie.Expires = DateTime.Now.AddDays(2);
            Response.Cookies.Add(cookie);
        }

        if (!String.IsNullOrEmpty(studPropSucc.Id))
        {
            Response.Redirect("~/Divisions.aspx?2");
        }
        else if(isFaulty)
        {

        }
        else
        {
            lblStatus.Text += "Enten er brugeren eller kodeordet forkert.";
        }
        lblStatus.Text += "</a>";
    }
}